package com.mashibing.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author SNA
 * @date 2025/9/5
 * @Description  将对象转化为JSON的工具类
 **/
public class JsonUtil {

    // 使用了 Jackson 库中的 ObjectMapper 类来实现对象与 JSON 之间的转换。需要导入Jackson依赖
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String objToJson(Object obj) {
        try {
            // objectMapper.writeValueAsString(channel): 将channel对象序列化为json字符串
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException("转换JSON失败！");
        }
    }

}
